// This file is part of the Pascal Compiler source code
//
// Copyright (C) 2009 UFPB (http://www.di.ufpb.br),
// Federal University of Paraiba.
// All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301, USA.
// *****************************************************************

/*
 * File: Controller.java
 */

package pascalcompiler.core;

import java.util.Calendar;
import java.util.GregorianCalendar;
import pascalcompiler.PascalCompilerView;
import pascalcompiler.exceptions.DuplicatedException;
import pascalcompiler.exceptions.ExpectedException;
import pascalcompiler.exceptions.FatalException;
import pascalcompiler.exceptions.UndefinedException;

/**
 *
 * @author Erisvaldo
 */
public class Controller {

    private static Complicador compiler;
    private static Controller instance;

    private Controller() {
        compiler = null;
    }

    public static Controller getInstance() {
        if (instance == null)
            instance = new Controller();

        return instance;
    }

    public Complicador getCompiler() {
        return compiler;
    }

    public void generateX86Code(String inputCode, PascalCompilerView view) {
        compiler = Complicador.getInstance();

        try {
            compiler.checkCode(inputCode);
            addOutputText("Sucesso", view);
        }
        catch(Exception ex) {
            addOutputText("Linha " + compiler.getLineCount() + ": " + ex.getMessage(), view);
        }
        
        

        String prefix = "[80x86] ";

//        try {
//            String code = compiler.getCode(inputCode);
//            addOutputText(prefix + "Código compilado com sucesso.", view);
//        } catch (DuplicatedException ex) {
//            addOutputText(prefix + "ERRO! Duplicado: " + ex.getMessage(), view);
//        } catch (ExpectedException ex) {
//            addOutputText(prefix + "ERRO! Esperado: " + ex.getMessage(), view);
//        } catch (FatalException ex) {
//            addOutputText(prefix + "ERRO: " + ex.getMessage(), view);
//        } catch (UndefinedException ex) {
//            addOutputText(prefix + "ERRO! Indefinido: " + ex.getMessage(), view);
//        }
    }

    private void addOutputText(String outputText, PascalCompilerView view) {
        // Get current time
        GregorianCalendar gc = new GregorianCalendar();
        String currentTimeString = gc.get(Calendar.HOUR_OF_DAY) + ":" + gc.get(Calendar.MINUTE) + ":" + ( gc.get(Calendar.SECOND) < 10 ? "0" + gc.get(Calendar.SECOND) : gc.get(Calendar.SECOND) );
        // Add text to the output area
        String currentOutputText = view.getOutputTextArea().getText();
        view.getOutputTextArea().setText(currentOutputText + "[" + currentTimeString + "] " + outputText + "\n");
    }
}